/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pe.siass.action;

import com.pe.siass.beans.ParametrosBean;
import com.pe.siass.bussines.ConsultarDaoImpl;
import com.pe.siass.form.FLoginUser;
import com.pe.siass.utils.UGraficos;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import nl.captcha.Captcha;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Hrengifo
 */
public class ALoginUser extends DispatchAction{

    public ActionForward validarUsuario(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        FLoginUser FLoginUser = (FLoginUser)form;
        String forward = "";

        ConsultarDaoImpl p = new ConsultarDaoImpl();

        System.out.println(FLoginUser.getStrLogin());
        System.out.println(FLoginUser.getStrPassword());
        System.out.println(FLoginUser.getStrCaptcha());

        Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
        if (captcha.isCorrect(FLoginUser.getStrCaptcha())){
             System.out.println("PASO EL CAPTCHA ==> TIENE KE INGRESAR A VALIDAR EL USUARIO");
             ParametrosBean pa = new ParametrosBean();
             List <ParametrosBean>  ls = p.selectParametros(pa);

             System.out.println(ls.size());

             forward = "exito";
        }else{
            forward = "error";
        }

        System.out.println("Finalizo");
        return mapping.findForward(forward);
   }


    public ActionForward generarGraficoEjemplo(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {

         response.setContentType("image/jpeg");
         OutputStream out = response.getOutputStream();
         UGraficos uGraficos= new UGraficos();
        //--------------------------------------------------
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(6D, "columna 1", "fila 1");
        dataset.setValue(3D, "columna 2", "fila 1");
        dataset.setValue(7D, "columna 1", "fila 2");
        dataset.setValue(9D, "columna 2", "fila 2");
        dataset.setValue(8D, "columna 1", "fila 3");
        dataset.setValue(7D, "columna 2", "fila 3");

        JFreeChart grafico = uGraficos.generaGraficoBarras("TITULO","VALR X","VALOR Y", dataset,true, true, false, Color.WHITE, Color.GRAY, 1.0F );
        ChartUtilities.writeChartAsJPEG(out, grafico, 500, 300);
        
        return mapping.findForward("grafico");

    }


}
